//
//   ______    ______    ______    
//  /\  __ \  /\  ___\  /\  ___\   
//  \ \  __<  \ \  __\_ \ \  __\_ 
//   \ \_____\ \ \_____\ \ \_____\ 
//    \/_____/  \/_____/  \/_____/ 
//
//  Powered by BeeFramework
//
//
//  AppBoard_iPhone.m
//  DingDing
//
//  Created by LHL on 14/11/20.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "AppBoard_iPhone.h"
#import "Board_iPhone_Login.h"
#import "MainNearBy_Board.h"
#import "AddPet.h"
#pragma mark -

@implementation AppBoard_iPhone{
    UIAlertView* _alert;
}

DEF_SINGLETON( AppBoard_iPhone )
DEF_SIGNAL( TAB_BETHOME )
DEF_SIGNAL( TAB_NEARBY )
DEF_SIGNAL( TAB_ADDPET)
- (void)load
{
}

- (void)unload
{
}

#pragma mark Signal

ON_CREATE_VIEWS( signal )
{
    [self observeNotification:BeeNetworkReachability.WIFI_REACHABLE];
    [self observeNotification:BeeNetworkReachability.WLAN_REACHABLE];
    [self observeNotification:BeeNetworkReachability.UNREACHABLE];
    bee.ui.router[self.TAB_NEARBY] = [MainNearBy_Board class];
    [self.view addSubview:bee.ui.router.view];
    [bee.ui.router open:self.TAB_NEARBY animated:YES];
}

#pragma mark - 网络监控

ON_NOTIFICATION3( BeeReachability, WIFI_REACHABLE, notification )
{
    //	[self presentMessageTips:@"使用wifi"];
    NSDictionary *dic =  [[EaseMob sharedInstance].chatManager loginInfo];
    if (dic == nil) {
        if (!([[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"] == nil ||[[[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"] isEqualToString:@""])) {
            [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:[[NSUserDefaults standardUserDefaults] objectForKey:@"im_username"] password:[[NSUserDefaults standardUserDefaults]objectForKey:@"im_password"]  completion:
             ^(NSDictionary *loginInfo, EMError *error) {
                 if (loginInfo && !error) {
                     [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
                     EMPushNotificationOptions *option = [[EMPushNotificationOptions alloc]init];
                     option.displayStyle = ePushNotificationDisplayStyle_simpleBanner;
                     [[EaseMob sharedInstance].chatManager setNickname:[[NSUserDefaults standardUserDefaults] objectForKey:@"petname"]];
                     [[EaseMob sharedInstance].chatManager asyncUpdatePushOptions:option];
                 }else {
                     switch (error.errorCode) {
                         case EMErrorServerNotReachable:
                             TTAlertNoTitle(@"连接服务器失败!");
                             break;
                         case EMErrorServerAuthenticationFailure:
                             TTAlertNoTitle(@"用户名或密码错误");
                             break;
                         case EMErrorServerTimeout:
                             TTAlertNoTitle(@"连接服务器超时!");
                             break;
                         default:
                             TTAlertNoTitle(@"登录失败");
                             break;
                     }
                 }
             } onQueue:nil];
        }
    }
//    NSLog(@"%@",dic);
}

ON_NOTIFICATION3( BeeReachability, WLAN_REACHABLE, notification )
{
    //	[self presentMessageTips:@"使用wlan"];
    NSDictionary *dic =  [[EaseMob sharedInstance].chatManager loginInfo];
    if (dic == nil) {
        if (!([[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"] == nil ||[[[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"] isEqualToString:@""])) {
            [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:[[NSUserDefaults standardUserDefaults] objectForKey:@"im_username"] password:[[NSUserDefaults standardUserDefaults]objectForKey:@"im_password"]  completion:
             ^(NSDictionary *loginInfo, EMError *error) {
                 if (loginInfo && !error) {
                     [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
                     EMPushNotificationOptions *option = [[EMPushNotificationOptions alloc]init];
                     option.displayStyle = ePushNotificationDisplayStyle_simpleBanner;
                     [[EaseMob sharedInstance].chatManager setNickname:[[NSUserDefaults standardUserDefaults] objectForKey:@"petname"]];
                     [[EaseMob sharedInstance].chatManager asyncUpdatePushOptions:option];
                 }else {
                     switch (error.errorCode) {
                         case EMErrorServerNotReachable:
                             TTAlertNoTitle(@"连接服务器失败!");
                             break;
                         case EMErrorServerAuthenticationFailure:
                             TTAlertNoTitle(@"用户名或密码错误");
                             break;
                         case EMErrorServerTimeout:
                             TTAlertNoTitle(@"连接服务器超时!");
                             break;
                         default:
                             TTAlertNoTitle(@"登录失败");
                             break;
                     }
                 }
             } onQueue:nil];
        }
    }
}

ON_NOTIFICATION3( BeeReachability, UNREACHABLE, notification )
{
    //	[self presentFailureTips:@"暂无网络"];
    //UIAlertView 一秒之后  自动消失
    _alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无网络" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
    [_alert show];
}

-(void) performDismiss:(NSTimer *)timer
{
    [_alert dismissWithClickedButtonIndex:0 animated:NO];
}


ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
    bee.ui.router.view.frame = CGRectMake( 0, 0, self.viewBound.size.width, self.viewBound.size.height );
}

ON_WILL_APPEAR( signal )
{
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

@end
